<?php
/**
 * 公会好友导入
 * 
 * @author 	  jiaowei<jiaowei@178.com>
 * @version   $Id: guild.php 10831 2010-09-15 06:51:33Z jiaowei $
 */
class friends_controller_guild extends Controller
{	
	private $_guild = null;
	
	public function __construct($app, $controller)
	{
		parent::__construct($app, $controller);
		$this->headerView = $this->viewDir."/_header.phtml";
		$this->_guild = new friends_model_guild();
		$this->requireLogin();
	}
	
	/**
	 * 公会会员列表
	 *
	 * @display true
	 */
	public function index_action()
	{
		$uid  = self::$login['uid'];
		$this->page = _('page', 1);
		$this->tem = $this->_guild->getUsersByPage($uid, $this->page, friends_model_guild::PAGE_SIZE);
		$this->pageInfo = $this->_genPages($this->tem['INFO']);
	}
	
	/**
	 * 分页获取会员列表
	 *
	 * @display false
	 */
	public function user_list_action()
	{
		$uid  = self::$login['uid'];
		$this->page = _('page', 1);
		$this->tem = $this->_guild->getUsersByPage($uid, $this->page, friends_model_guild::PAGE_SIZE);
		$this->pageInfo = Tag_Parser::translate($this->_genPages($this->tem['INFO']));//解析178标签
		ob_start();
		include_once($this->viewDir . '/user_list.phtml');
		$data = ob_get_contents();
		ob_end_clean();
		exit($data);
	}
	
	/**
	 * 添加好友表单
	 *
	 * @display false
	 */
	public function prepare_action()
	{
		$this->_renderJson();
		
		$search = $this->add_uid;//要搜索的用户名
		$mod = Model::factory('index_model_standardprop');
		$uid = $mod->search($search);
		
		if (!$uid) {//是否存在
			$this->ret = self::failure('您搜索的用户不存在');
			return;
		}
		
		$hid = (int) self::$login['uid'];//host_id
		
		//同公会检测
		if (!$this->_guild->checkGuildUser($hid, $uid)) {
			$this->ret = self::failure('只有同一个公会会员才能加他为好友。');
			return;
		}
		
		try {
			$username = $this->_mod->checkFriend($hid, $uid);
			$this->ret = self::success(array(
				'uid'	   => $uid,
				'username' => $username,
				'the'	   => $this->getThe($uid),
				'avatar'   => avatar_util_saver::get_path($uid, 50)
			));
		} catch (Exception $err) {
			$code = $err->getMessage();
			$this->ret = self::failure(friends_util_exception::$errorDescriptions[$code]);
		}
	}
	
	/**
	 * 发送邀请
	 *
	 * @display false
	 */
	public function invite_action()
	{
		$this->_renderJson();
		$uid  = self::$login['uid'];
		
		//被邀请会员ID列表
		$postInviters = _('uids', '');

		if (!$postInviters) {
			$this->ret = self::failure(Error::ERROR_WRONG_PARAMS, '邀请会员不能为空');
			return ;
		}
		
		//过滤邀请会员id
		$postInviters = explode(',', $postInviters);
		$inviters = array();
		foreach ($postInviters AS $k => $v) {
			$v = (int) $v;
			if ($v) {
				$inviters[] = $v;
			}
		}
		
		if (!$inviters) {
			$this->ret = self::failure(Error::ERROR_WRONG_PARAMS, '邀请会员不能为空');
			return ;
		}

		//添加好友关系
		$this->_guild->addFriends($uid, $inviters);
		
		//发送短信
		$data = array(
			'uid' => implode(',', $inviters),
			'username' => '',
			'host_id' => self::$login['uid'],
			'host_name' => self::$login['nickname'],
			'message'=> '同公会会员邀请',
			'type'=> 'add'
		);
		
		$a = $_SERVER['LOCALE']['apps'][$this->app];
		$res = sms_util_system::send(null, $data['uid'],
			eval_template($a[1]['message'][$data['type']], $data),
			$this->app == 'friends' ? ($this->app . '|' . $data['type']) : $this->app,
			$data,
			$_SERVER['REQUEST_TIME']
		);
		
		//修改邀请状态
		$this->_guild->addInvitedLog($uid, $inviters);
		
		$this->addMessage('发送邀请成功');
		$this->ret = self::success();
	}

	/**
	 * 分页
	 *
	 * @param array $pageinfo
	 * @param array $params
	 * @return str
	 * 
	 * @display false
	 */
	private function _genPages($pageinfo, $params=array())
	{
		$ret = '';
		$current = $pageinfo['PAGE'];
		$pages = array();
		$pages[] = '<i:page label="上一页" value="'.($current-1).'" style="previous"/>';
		
		$temp = array();
		for($i=$current-2; $i<=$current+2; $i++){
			if($i>0 and $i<=$pageinfo['PAGECOUNT']){
				$temp[] = $i;
			}
		}
		
		foreach ($temp as $i=>$p){
			if($i==0){
				if($p - 1 > 0){
					$pages[] = '<i:page label="1" value="1"/>';
				}
				if($p - 1 > 1){
					$pages[] = '<i:page style="sep"/>';
				}
			}
			$pages[] = '<i:page label="'.$p.'" value="'.$p.'"/>';
			if($i==count($temp)-1){
				if($pageinfo['PAGECOUNT'] - $p>1){
					$pages[] = '<i:page style="sep"/>';
				}
				if($pageinfo['PAGECOUNT'] - $p>0){
					$pages[] = '<i:page label="'.$pageinfo['PAGECOUNT'].'" value="'.$pageinfo['PAGECOUNT'].'"/>';
				}
			}
		} 
		
		$pages[] = '<i:page label="下一页" value="'.($current+1).'" style="next"/>';
		//$page_url = self::_makeURL($this->app, $this->controller, $this->action, $params)."/page/[#page]";
		$page_url = "javascript:show_page('[#page]')";
		return '<i:pages baseurl="'.$page_url.'" current="'.$current.'" pagecount="'.$pageinfo['PAGECOUNT'].'" delimiter="..." xmlns:i="178">'
			. implode("", $pages). "</i:pages>";
	}
}
